import { defineStore } from 'pinia';
import { useUserStore } from '@/store/system/user';
import { setTreeData, montageStr, mergeAndUniqueObjects, deepClone } from '@/utils/tools-util';
import { rolesApi } from '@/api/system/roles/roles-api';
import { getViews } from '@/utils/menucom-util';
import { ROUTE_NAME } from '@/utils/verify-util';
import { homeRouters } from '../../router/system/home';


export const menuStore = defineStore('menuStore', {
    state: () => ({
        menuList: [],
        // 展开/收缩
        isCollapse: false,
    }),

    getters: {
    },

    actions: {
        async getMenuList() {
            let dataMenus = [];
            let dataAll = [];
            let roleIds = montageStr(useUserStore().roles, 'id');
            let params = {
                id: roleIds,
                state: true
            };
            const res = await rolesApi.findRoles(params);
            res.data.forEach(item => {
                item.menus.forEach(el => {
                    dataAll.push(el);
                });
            });
            // 去除重复项（多个角色重复的路由）
            res.data.length >= 2 ? dataMenus = mergeAndUniqueObjects(dataAll, 'id') : dataMenus = dataAll;
            this.menuList = setTreeData(dataMenus, 'id', 'parentId', 'children');
            let homeRoute = homeRouters[0].children[0];
            this.menuList.unshift({
                path: '/'+homeRoute.path,
                title: homeRoute.meta.title,
                name: homeRoute.name
            });
        },

        getDynamicRoute(router) {
            // let menus = this.menuList;
            // this.menuList = menus.filter(item => item.name != menus[0].name);
            let menuClone = deepClone(this.menuList);
            menuClone = menuClone.filter(item => item.name != menuClone[0].name);
            menuClone.forEach(item => {
                if(item.path) {
                    let routeItem = {
                        path: item.path,
                        name: item.path,
                        meta: {
                            title: item.title
                        },
                        component: item.component,
                        children: []
                    }
                    if(item.children && item.children.length) {
                        item.children.forEach(subItem => {
                            routeItem.children.push({
                                path: subItem.path,
                                component: getViews((`../views/${subItem.component}.vue`)),
                                name: subItem.path.match(ROUTE_NAME)[1],
                                meta: {
                                    title: subItem.title
                                }
                            })
                        })
                    }
                    router.addRoute('_home',routeItem);
                };
            });
        }
    },

    persist: [
        {
            paths: ['menuList'],
            storage: sessionStorage,
        },
    ],
})